package org.tech4j.common.core.request.param;

import com.google.common.collect.Maps;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.ArrayUtils;
import org.tech4j.common.spi.Join;

import java.util.Map;

/**
 * @program: tech4j-common
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/2 20:08
 **/
@Join
public class QueryParameterData implements ParameterData {

    @Override
    public String builder(String paramName, HttpServletRequest request) {
        return request.getParameter(paramName);
    }

    @Override
    public Map<String, Object> builderMap(String paramName, HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        if (parameterMap == null || parameterMap.isEmpty()) {
            return null;
        }
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(parameterMap.size());
        parameterMap.forEach((k, v) -> {
            map.computeIfPresent(k, (key, value) -> ArrayUtils.isEmpty(v) ? "" : v[0]);
        });
        return map;
    }
}
